<!--
 * @Author: your name
 * @Date: 2022-03-16 20:30:20
 * @LastEditTime: 2022-04-27 01:43:47
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \react-antd-admin\vue-admin-template\src\views\admin\privilegeList.vue
-->
<template>
  <div>
    <el-button type="primary"
               size='mini'
               @click="toAdd">新增</el-button>
    <div class='table'>
      <el-table :data="treeData"
                v-loading='loading'
                row-key="id"
                size='mini'
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                style="width: 100%">
        <el-table-column prop='name'
                         label='名称'>
        </el-table-column>
        <el-table-column width="80px"
                         prop='id'
                         label='序号'>
        </el-table-column>
        <el-table-column prop='route'
                         label='路径'>
        </el-table-column>
        <el-table-column prop='type'
                         label='类型'>
        </el-table-column>
        <el-table-column label='状态'>
          <template slot-scope="scope">
            <el-tag :type="scope.row.hidden === 0 ? 'success': 'danger'">{{scope.row.hidden === 0 ? '显示' : '隐藏'}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作"
                         width="100">
          <template slot-scope="scope">
            <!--  -->
            <el-button type="text"
                       size="small"
                       @click="toEdit(scope.row)">编辑</el-button>
            <el-divider direction="vertical"></el-divider>
            <!-- " -->
            <el-button style="color:red"
                       type="text"
                       size="small"
                       @click="toDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div>
      <el-dialog :title="title"
                 :visible.sync="visible"
                 @close="toClose">
        <el-form ref="ruleForm"
                 :model="form"
                 :rules="rules">
          <el-form-item prop="name"
                        label="名称"
                        :label-width="formLabelWidth">
            <el-input v-model="form.name"
                      autocomplete="off" />
          </el-form-item>
          <el-form-item prop="route"
                        label="路径"
                        :label-width="formLabelWidth">
            <el-input v-model="form.route"
                      autocomplete="off" />
          </el-form-item>
          <el-form-item prop="parent_id"
                        label="父权限"
                        :label-width="formLabelWidth">
            <el-cascader v-model="form.parent_id"
                         placeholder="请选择父权限"
                         :show-all-levels="false"
                         :options="options"
                         :props="{ checkStrictly: true }"
                         clearable
                         @change="handleChange">
            </el-cascader>
          </el-form-item>
          <el-form-item prop="description"
                        label="描述"
                        :label-width="formLabelWidth">
            <el-input v-model="form.description"
                      autocomplete="off" />
          </el-form-item>
          <el-form-item v-if="title === '修改权限信息'"
                        prop="hidden"
                        label="状态"
                        :label-width="formLabelWidth">
            <el-switch v-model="form.hidden"
                       active-color="#13ce66"
                       inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer"
             class="dialog-footer">
          <el-button @click="visible=false">取 消</el-button>
          <el-button type="primary"
                     @click="toSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPrivilegeTree, createPrivilege, deletePrivilegeById, updatePrivilege } from '@/api/privilege';
import { getFindRole } from '@/api/role';
export default {
  data() {
    return {
      treeData: [],
      options: [],
      roles: [],
      loading: false,
      title: '',
      visible: false,
      formLabelWidth: '80px',
      form: {},
      rules: {
        name: [{
          required: true,
          message: '请输入名称',
          trigger: 'blur'
        }],
        route: [{
          required: true,
          message: '请输入路径',
          trigger: 'blur'
        }],
        role_id: [{
          required: true,
          message: '请选择角色',
          trigger: 'blur'
        }],
      },
    };
  },
  watch: {
  },
  created() {
    this.getTreeData();
    this.getAllRole()
  },
  methods: {
    async getTreeData() {
      this.loading = true;
      const { data } = await getPrivilegeTree();
      this.treeData = data;
      for (const item of data) {
        if (item.children.length !== 0) {
          this.options.push({ value: item.id, label: item.name, children: getNode(item.children) })
        } else {
          this.options.push({ value: item.id, label: item.name })
        }
      }
      function getNode(children) {
        const node = [];
        for (const item of children) {
          if (item.children.length !== 0) {
            node.push({ value: item.id, label: item.name, children: getNode(item.children) })
          } else {
            node.push({ value: item.id, label: item.name })
          }
        }
        if (node.length === 0) return [];
        return node;
      };
      this.loading = false;
    },
    async getAllRole() {
      const res = await getFindRole();
      this.roles = res.data;
    },
    toAdd() {
      this.form = {}
      this.title = '添加权限信息'
      this.visible = true
    },
    toEdit(row) {
      this.form = { ...row }
      this.title = '修改权限信息'
      this.visible = true
    },
    handleChange(value) {
      this.form.parent_id = value[value.length - 1]
    },
    toClose() {
      this.$refs.ruleForm.resetFields()
    },
    toSave() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let res;
          if (this.title === '添加权限信息') {
            res = await createPrivilege(this.form)
          }
          if (this.title === '修改权限信息') {
            res = await updatePrivilege(this.form)
          }
          this.$notify.success({
            title: '成功',
            message: res.message
          })
          this.getTreeData()
          this.visible = false
        } else {
          return false
        }
      })
    },
    toDelete(id) {
      this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await deletePrivilegeById({ id })
        this.$message({ type: 'success', message: res.message })
        this.getTreeData()
      })
    },
  }
};
</script>

<style scoped lang="  <div></div>">
</style>
